<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Personal extends Admin_Controller
{

    public function profile()
    {
        $uid = $this->session->userdata('user_id');
        $member = $this->user_m->get_user_by_user_id($uid);
        $data['member'] = $member;
        $this->load->view('personal/profile', $data);

    }

    public function update()
    {

        $post = $this->input->post();
        $uid = isset($post['user_id']) ? intval($post['user_id']) : false;
        $username = isset($post['user']) ? htmlspecialchars($post['user'], ENT_QUOTES) : '';
        $getpassword= $this->input->post('user_password',true);
        if ($uid) {
            // 修改
            $admin = array(
                'user_sex' =>$post['user_sex'],
                'user_tel' =>$post['user_tel'],
                'user_qq' =>$post['user_qq'],
                'user_email' =>$post['user_email'],
            );
            if(!empty($username)){
                $admin['user_name'] = $username;
            }
            if(!empty($getpassword)){
                $admin['user_salt'] = $salt = get_salt();
                $admin['user_password'] = password_dohash($getpassword,$salt);
            }
            $this->user_m->update_user($uid,$admin);
            show_message('修改个人资料成功',site_url(X_URL.'home/index'),1);
        }else{
            show_message('参数缺失',site_url(X_URL.'personal/profile'),1);
        }


    }
    public function myloan($page=1){
        $uid = $this->session->userdata('user_id');
        $member = $this->user_m->get_user_by_user_id($uid);
        $this->load->model('moneysale_m');
        $this->load->model('order_m');
        $limit = 30;
        $this->load->library('pagination');
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/member/xiaoshoudai/'.$uid);
        $config['total_rows'] = $this->db->where("user_id",$uid)->count_all('user_money_sale');
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
        $logs = $this->moneysale_m->get_all_user_money_sale($start,$limit,$uid);
        if(!empty($logs)){
            foreach ($logs as $key => $val){
                $orders = $this->order_m->get_shop_orders_by_order_no($val['order_no']);
                $logs[$key]['order_id'] = $orders['order_id'];
            }
        }
        $data['logs'] = $logs;
        $data['member'] = $member;
        $this->load->view('personal/sale_info', $data);
    }

    public function myaddr(){
        $uid = $this->session->userdata('user_id');
        // 地址
        $dress=$this->member_m->get_address_by_uid($uid);
        if(!empty($dress)){
            foreach($dress as $key=>$val){
                $area =  $this->areas_m->get_area_by_id($val['dress_province']);
                $area2 =  $this->areas_m->get_area_by_id($val['dress_city']);
                $area3 =  $this->areas_m->get_area_by_id($val['dress_district']);
                $dress[$key]['dress_city'] = $area['area_name'].$area2['area_name'].$area3['area_name'];
            }
        }
        $data['dress'] = $dress;
        $this->load->view('personal/myaddr', $data);
    }
}